#@layoutT("代码生成器")
#define css()
<style type="text/css">
.layui-btn-primary{
	border:0px;
}
 .layui-btn-primary:hover{
    color: #fff;
    border-color: #1e9fff;
    background: #1e9fff;
}
.end{
    cursor: pointer;
    color: #fff;
    background: #1e9fff;
}
</style>

#end
#define main()
<form action="javascript:void(0)" class="layui-form layui-form-pane f-form" method="post">
<div class="layui-row layui-col-space1 task-row">
	#@colStart("主表",4)
		<input type="text" class="layui-input" name="tableName"  readonly="readonly" placeholder="请选择主表"/>
	#@colEnd()
	###@colStart("主表实体名称",3)
		<input type="hidden" class="layui-input" name="modelName" value="" readonly="readonly"/>
	###@colEnd()
	#@colStart("主表关联字段",4)
		<input type="text" class="layui-input" name="tableColumn" placeholder="关联字段"/>
	#@colEnd() 	
	#@colStart("作者名称",4)
		<input type="text" class="layui-input" name="authorName" value="qinhailin"/>
	#@colEnd()
</div>
<div class="layui-row layui-col-space1 task-row">
	#@colStart("子表",4)
		<input type="text" class="layui-input" name="subTableName" placeholder="请选择从表"/>
	#@colEnd()
	###@colStart("子表实体名称",3)
		<input type="hidden" class="layui-input" name="subModelName" value="" readonly="readonly"/>
	###@colEnd()
	#@colStart("子表关联字段",4)
		<input type="text" class="layui-input" name="subTableColumn" placeholder="关联字段"/>
	#@colEnd()
	#@colStart("package",4)
		<input type="text" class="layui-input" name="packageName" value="com.qinhailin.portal.business"/>
	#@colEnd()
</div>

<div class="layui-row layui-col-space12 task-row" style="text-align: center">
 	<button class="layui-btn  layui-btn-normal" id="btnGenCode">生成代码 »</button>
 	<button class="layui-btn  layui-btn-normal layui-disabled" id="downloadAll" disabled="disabled">下载代码</button>
</div>

<div class="layui-row layui-col-space task-row">
 	#@colStart("后端代码",12)
 		<div id="btnGroupJava">
			<button  class="btn layui-btn layui-btn-primary" id="Controller.java" onclick="chilckBtn(this)" >Controller.java</button>
	        <button  class="btn layui-btn layui-btn-primary" id="Service.java" onclick="chilckBtn(this)">Service.java</button>
			<button  class="btn layui-btn layui-btn-primary" id="Model.java" onclick="chilckBtn(this)">Model.java</button>
	        <button  class="btn layui-btn layui-btn-primary" id="BaseModel.java" onclick="chilckBtn(this)">BaseModel.java</button>
	    </div>
	#@colEnd()
	</div>

<div class="layui-row layui-col-space1 task-row">
 	#@colStart("前端代码",12)
 		<div id="btnGroupHtml">
	         <button class="btn layui-btn layui-btn-primary" id="index.html" onclick="chilckBtn(this)">index.html</button>
	         <button class="btn layui-btn layui-btn-primary" id="add.html" onclick="chilckBtn(this)">add.html</button>
	         <button class="btn layui-btn layui-btn-primary" id="edit.html" onclick="chilckBtn(this)">edit.html</button>
			 <button class="btn layui-btn layui-btn-primary" id="_form.html" onclick="chilckBtn(this)">_form.html</button>
	    </div>
	#@colEnd()
</div>
<div class="layui-row layui-col-space1 task-row">
 	<textarea id="genCodeArea" class="layui-textarea" style="display: none;"></textarea>
</div>
</form>
#end
#define js()
<script>
//单击事件
function chilckBtn(dom) {
   var collection = $(".layui-btn-primary");
   $.each(collection, function () {
       $(this).removeClass("end");
   });
   $(dom).addClass("end");
}
var index=0,isSubTable=false;
$('input[name=tableName]').click(function(){
	isSubTable=false;
	index=openDialog('选择主表','#(path)/portal/generator/code/tables',false,'50%',null,null);
});
$('input[name=subTableName]').click(function(){
	isSubTable=true;
	index=openDialog('选择子表','#(path)/portal/generator/code/tables',false,'50%',null,null);
});

function getTable(data){
	var packageName=$('input[name=modelName]').val();
	var tables="";
	var modelNames="";
	for(var i=0;i<data.length;i++){
		if(tables!=""){
			tables+=",";
			modelNames+=",";
		}
		tables+=data[i].name;
		modelNames+=data[i].modelName;
	}
	if(isSubTable){
		$('input[name=subTableName]').val(tables);
		$('input[name=subModelName]').val(modelNames);
	}else{
		$('input[name=tableName]').val(tables);
		$('input[name=tableColumn]').val(data[0].primaryKey);
		$('input[name=modelName]').val(modelNames);
	}
	layer.close(index);
}
    $(function () {
        var genCodeArea = CodeMirror.fromTextArea(document.getElementById("genCodeArea"), {
            lineNumbers: true,
            matchBrackets: true,
            mode: "text/x-sql",
            lineWrapping:true,
            readOnly:true,
            foldGutter: true,
            gutters:["CodeMirror-linenumbers", "CodeMirror-foldgutter"]
        });
        genCodeArea.setSize('auto','auto');
	    
		var codeData;
        /**
         * 生成代码
         */
        $('#btnGenCode').click(function ()  {
            if($("input[name=tableName]").val()==""
               ||$("input[name=subTableName]").val()==""){
				 warn("请选择数据库表");
				 return;
             };
             if($("input[name=subTableColumn]").val()==""){
            	 warn("子表关联字段不能为空");
 				 return;
             }
            
            $.ajax({
                type: 'POST',
                url: "#(path)/portal/generator/code/createSubTableCode",
                data: {
                    "tableName": $("input[name=tableName]").val(),
                    "modelName":$("input[name=modelName]").val(),
                    "tableColumn": $("input[name=tableColumn]").val(),
                    "subTableName":$("input[name=subTableName]").val(),
                    "subTableColumn": $("input[name=subTableColumn]").val(),
                    "subModelName":$("input[name=subModelName]").val(),
                    "authorName":$("input[name=authorName]").val(),
                    "packageName":$("input[name=packageName]").val()
                },
                dataType: "json",
                success: function (data) {
                    if (data.state == "ok") {
                    	success("代码生成成功");
                        codeData = data.data[0];
                        var codeJava=codeData["codeJava"]||[];
                        var codeHtml=codeData["codeHtml"]||[];
						var btn1="",btn2="",checkbox1="",checkbox2="";
                        
                        codeJava.forEach(v=>{                            
                        	btn1+='<button  class="btn layui-btn layui-btn-primary " onclick="chilckBtn(this);" id="'+v+'" >'+v+'</button>';
                        	checkbox1+='<input lay-filter="code" type="checkbox" name="javaCode" onclick="chilckBtn(this);" id="'+v+'" value="'+v+'" title="'+v+'" />';
                        });
                        codeHtml.forEach(v=>{  
                        	btn2+='<button  class="btn layui-btn layui-btn-primary" onclick="chilckBtn(this);" id="'+v+'" >'+v+'</button>';
                        	checkbox2+='<input lay-filter="code" type="checkbox" name="htmlCode" onclick="chilckBtn(this);" id="'+v+'" value="'+v+'" title="'+v+'" />';
                        });
                        
                        if(btn1!=""){                       	
                        	//btn1+='<button class="btn layui-btn layui-btn-primary" onclick="download(\'codeJava\')">下载</button>';
                        	checkbox1+='<button class="btn layui-btn layui-btn-primary" onclick="download(\'codeJava\')">下载</button>';
                        }
                        if(btn2!=""){
                        	//btn2+='<button class="btn layui-btn layui-btn-primary" onclick="download(\'codeHtml\')">下载</button>';
                        	checkbox2+='<button class="btn layui-btn layui-btn-primary" onclick="download(\'codeHtml\')">下载</button>';
                        }          
                                     
                        $("#btnGroupJava").html(checkbox1);
                       	$("#btnGroupHtml").html(checkbox2);
                       	layui.form.render();
                       	layui.form.on('checkbox(code)', function(data){
	                       	if (!$.isEmptyObject(codeData)) {
	                            genCodeArea.setValue(codeData[data.value]);
	                            genCodeArea.setSize('auto', 'auto');                                                     
	                        }
                       	});
                       	
                       	/* $('.btn').bind('click', function () {
                               if (!$.isEmptyObject(codeData)) {
                                   genCodeArea.setValue(codeData[this.id]);
                                   genCodeArea.setSize('auto', 'auto');                                                     
                               }
                           }); */
                                    
                        $('#downloadAll').removeClass('layui-disabled');                        
                        $('#downloadAll').attr("disabled",false);
                                          
                       	genCodeArea.setValue(codeData[codeJava[0]||"Controller.java"]);
                       	genCodeArea.setSize('auto', 'auto');                       
                    } else {     
                    	$("#btnGroupJava").html("");
                       	$("#btnGroupHtml").html("");   
                       	genCodeArea.setValue("");
                       	genCodeArea.setSize('auto', 'auto');                     
                        error(data.msg || '代码生成失败');
                    }
                }
            });
        });
    });
    
    $('#downloadAll').click(function(){
    	download("codeAll");
     });

    function download(type){
    	var text="所有";
        var codeName = [];
        if(type=="codeHtml"){
            $('input[type=checkbox][name=htmlCode]:checked').each(function() {
            	codeName.push($(this).val());
 	        });
            text=codeName.toString();
        }else if(type=='codeJava'){
	        $('input[type=checkbox][name=javaCode]:checked').each(function() {
	        	codeName.push($(this).val());
	        });
        	text=codeName.toString();
        }else{
        	codeName = ["all"];
        }
        
        if(type!='codeAll'&&codeName.length==0){
        	error("请选择要下载的文件")
        	return
        }
        
        jsConfirm("确定创建"+text+"文件吗？",function(){
        	$.ajax({
				url:"#(path)/portal/generator/code/download",  
			    type:"POST", 
			    data: {"type":type,"codeName":codeName.toString()},
			    success:function(ret){
			    	if(ret.state=='ok'){
			    		success("代码文件创建成功，请刷新项目!")
			    	}else{
			    		error("代码文件创建失败")
				    }
			    }	
			});  	
         });
    
   }
</script>
#end